<?php
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */
function error_handler($errno, $errstr, $errfile, $errline) {
    if (error_reporting () == 0)
        return;
    
    $mess = '';
    switch ($errno) {
        case E_USER_ERROR :
            $mess .= '<strong>My ERROR</strong> [' . $errno . ']' . $errstr;
            $mess .= ' Fatal error on line ' . $errline . ' in file ' . $errfile;
            $mess .= ', PHP ' . PHP_VERSION . ' (' . PHP_OS . ')';
            $mess .= ' Aborting...';
            if (IN_DEBUG) {
                echo $mess;
            }
            _log ( $mess );
            exit ( 1 );
            break;
        
        case E_USER_WARNING :
            $mess .= '<strong>WARNING</strong> [' . $errno . ']' . $errstr;
            $mess .= ' Fatal error on line ' . $errline . ' in file ' . $errfile;
            break;
        
        case E_USER_NOTICE :
            $mess .= '<strong>NOTICE</strong> [' . $errno . ']' . $errstr;
            $mess .= ' Fatal error on line ' . $errline . ' in file ' . $errfile;
            break;
        
        default :
            $mess .= 'Unknown error type: [' . $errno . ']' . $errstr;
            $mess .= ' Fatal error on line ' . $errline . ' in file ' . $errfile;
            break;
    }
    if (IN_DEBUG)
        echo $mess;
    _log ( $mess );
}

set_error_handler ( 'error_handler', E_ALL ^ E_NOTICE );

function _log($mess, $file = null, $line = null) {
    $fileName = 'error_' . date ( 'd-m-Y', time () ) . '.log';
    
    if ($file !== null) {
        $mess .= ' in ' . $file;
        $mess .= (($line !== null) ? ': line' . $line : '');
    }
    
    $content = "[" . date ( 'd/m/Y H:i:s', time () ) . ']' . $_SERVER ['SCRIPT_NAME'] . ' (' . $_SERVER ['REQUEST_TIME'] . ")\n\t" . 'IP: ' . $_SERVER ['REMOTE_ADDR'] . ' - ' . $_SERVER ['HTTP_USER_AGENT'] . "\n\t$mess\n";
    $handle = fopen ( LOG_FOLDER . $fileName, 'w' );
    fwrite ( $handle, $content );
    fclose ( $handle );
}  
?>
